<?php
require_once "./config.php";
require_once "./function.php";
$dataid=$_POST["dataid"];
$datanum=$_POST["datanum"];
$con=connect();

//方法1 循环遍历添加并循环遍历执行  
        // foreach ($dataid as $key => $value) {
        // $sql="update shopping set num={$datanum[$key]} where id={$value};";  
        // $result=mysqli_query($con,$sql);
        // }
        // print_r($sql);

//方法2 一次执行
//语句范例 UPDATE shopping SET num=CASE id WHEN 1 THEN 10 WHEN 2 THEN 10 END WHERE id IN (1,2)
        $sql="UPDATE shopping SET num=CASE id";
        foreach ($dataid as $key => $value) {
        $sql.=" WHEN {$value} THEN {$datanum[$key]}";
        }
        $sql.=" end where id in (".implode(',',$dataid).")";
        // print_r($sql);
        $result=mysqli_query($con,$sql);
        //接收
        $response=["code"=>0,"msg"=>"操作失败"];
        if ($result) {
            $response["code"]=1;
            $response["msg"]="操作成功";
        }
        echo json_encode($response);
?>